package com.pmy.controller;

import com.pmy.pojo.Provider;
import com.pmy.service.ProviderService;
import com.pmy.service.ProviderServiceImpl;
import com.pmy.util.PageSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
public class ProviderController{

    @Autowired
    ProviderServiceImpl providerService;
//  条件分页查询
    @RequestMapping(value = "/providerList")
    public String providerList(Model model, HttpServletRequest request){

        String queryProCode = request.getParameter("queryProCode");
        String queryProName = request.getParameter("queryProName");
        String pageIndex = request.getParameter("pageIndex");//当前页
        //设置页面容量
        int pageSize = 10;//写死页面容量
        //当前页
        int currentPageNo = 1;//第一次当前页写死为第一页
        if (queryProName == ""){
            queryProName = null;//如果为空字符串设置为Null
        }
        if (queryProCode == ""){
            queryProCode = null;
        }


        if (pageIndex != null){
            try {
                currentPageNo = Integer.parseInt(pageIndex);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }


        int totalCount = 0;//用户数据条数

        //获取用户总数
        totalCount = providerService.getProviderCount(queryProCode,queryProName);
        //总页数支持
        PageSupport pageSupport = new PageSupport();
        pageSupport.setCurrentPageNo(currentPageNo);
        pageSupport.setPageSize(pageSize);
        pageSupport.setTotalCount(totalCount);

        //控制首页和尾页、如果页面小于1了，就显示第一页
        int totalPageCount = pageSupport.getTotalPageCount();
        if (totalPageCount < 1){
            currentPageNo = 1;
        }else if(currentPageNo > totalPageCount){
            currentPageNo = totalPageCount;
        }


        //获取用户数据
        List<Provider> providerList = providerService.queryAll(queryProCode, queryProName, (currentPageNo - 1) * pageSize, pageSupport.getPageSize());

        //前端需要的值
        model.addAttribute("queryProCode",queryProCode);
        model.addAttribute("queryProName",queryProName);
        //查询的用户数据
        model.addAttribute("providerList",providerList);

        //分页支持
        model.addAttribute("totalPageCount",totalPageCount);
        model.addAttribute("totalCount",totalCount);
        model.addAttribute("currentPageNo",currentPageNo);
        model.addAttribute("totalPageCount",totalPageCount);

        return "providerlist";
    }
//    详细查询实现
    @RequestMapping(value = "seeProvider")
    public String seeProvider(String providerId,Model model){
        Provider provider = null;
        try {
            provider = providerService.getProviderById(Integer.parseInt(providerId));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }

        model.addAttribute("provider",provider);
        return "seeprovider";
    }
//    添加供应商实现
    @RequestMapping(value = "reachProvider")
    public String reachProvider(String providerId,Model model){
//          providerId为0说明是添加供应商
        if (Integer.parseInt(providerId) == 0){
            return "addprovider";
        }
//          providerId有值返回到修改页面
        Provider provider = null;
        try {
            provider = providerService.getProviderById(Integer.parseInt(providerId));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        model.addAttribute("provider",provider);

        return "updateprovider";
    }
    @RequestMapping(value = "addProvider")
    public String addProvider(Provider provider,HttpServletRequest request){
        int pro = 0;
        try {
            pro = providerService.addProvider(provider);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (pro == 1){
            request.getSession().setAttribute("msg1","添加成功");
        }else {
            request.getSession().setAttribute("msg0","添加失败");
        }

        return "redirect:/providerList";
    }

//    删除的实现
    @RequestMapping(value = "deleteProvider")
    public String deleteProvider(String providerId,HttpServletRequest request){

        int pro = 0;
        try {
            pro = providerService.deleteProviderById(Integer.parseInt(providerId));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }

        if (pro == 1){
            request.getSession().setAttribute("msg1","删除成功");
        }else {
            request.getSession().setAttribute("msg0","删除失败");
        }

        return "redirect:/providerList";
    }

//    修改的实现
    @RequestMapping(value = "/updateProvider")
    private String updateProvider(Provider provider,HttpServletRequest request){
        int pro= 0;
        try {
            pro = providerService.updateProviderById(provider);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (pro == 1){
            request.getSession().setAttribute("msg1","修改成功");
        }else {
            request.getSession().setAttribute("msg0","修改失败");
        }

        return "redirect:/providerList";
    }

}
